<?php

namespace app\common\model;

use app\common\util\Tools;
use think\Model;

/**
 * Class Auth
 * @package app\common\model
 * 后台账号表
 */
class trAdmin extends Model
{
    protected $pk = 'id';
    protected $resultSetType = 'collection'; //申明结果集是数据集对象（collection），查询结果才能用toArray()方法转为数组
    protected $name = "admin"; //表名

    protected $type = [
        'create_time'  =>  'timestamp:Y-m-d H:i',
    ];

    //获取单条信息
    public static function getAdminInfo($value, $field = "*"){
        if(is_array($value)){
            $filter = [];
            if(!empty($value['id'])){
                $filter['id'] = $value['id'];
            }
            if(!empty($value['username'])){
                $filter['username'] = $value['username'];
            }
            $adminData =  self::where($filter)->field($field)->find();
        }else{
            $adminData =  self::where("username|id", $value)->field($field)->find();
        }
        if(!empty($adminData)){
            return Tools::transferFields($adminData->toArray());
        }
        return [];
    }

    //生成token
    public static function generateSessionId(){
        return Tools::createGUID();
    }
}